package com.yanq.liancar.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.vondear.rxtool.RxSPTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.MyCarAdapter;
import com.yanq.liancar.bean.BaseBean;
import com.yanq.liancar.bean.MyCar;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyCarActivity extends BaseToolbarActivity {

    @BindView(R.id.rv_myCar)
    RecyclerView mRecyclerview;
    @BindView(R.id.mycar_tip)
    TextView myCarTip;
    @BindView(R.id.tv_bangdingCar)
    TextView tv_bangdingCar;
    @BindView(R.id.rl_noCar)
    LinearLayout noCar;
    @BindView(R.id.ll_car)
    LinearLayout llCar;

    private List<MyCar.DataBean> mMyCars = new ArrayList<>();
    private MyCarAdapter mMyCarAdapter;
    private boolean isC;
    private boolean isClear = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_car);
        Utils.setStatusBar(this, true, false);
        ButterKnife.bind(this);
        mTvTitle.setText("我的车辆");
        isC = getIntent().getBooleanExtra("isC", false);

        mIvRight.setImageResource(R.mipmap.tianjia);
        mIvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RxSPTool.putString(mContext, "SelectAddressTitle", "京A");
                // TODO: 2018/5/30跳转到添加车辆上去,应该添加上绑定车辆数量的判断，为了效果先不加了
                if (mMyCars.size() < 3) {
                    Intent intent = new Intent(mContext, BindCarActivity.class);
                    startActivity(intent);
                } else {
                    RxToast.warning("最多可添加三辆");
                }
            }
        });

        initRecyclerview();
    }

    private void initRecyclerview() {
        mRecyclerview.setNestedScrollingEnabled(false);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mMyCarAdapter = new MyCarAdapter(R.layout.item_mycar, mMyCars);
        mRecyclerview.setAdapter(mMyCarAdapter);

        mMyCarAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                initOkGoCheckCar(mMyCars.get(position).getId(), position);
            }
        });

        mMyCarAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                int carid = mMyCars.get(position).getId();
                Bundle bundle = new Bundle();
                bundle.putString("carId", carid + "");
                Intent intent = new Intent(getApplicationContext(), MyCarInfoActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initOkGo();
    }

    //获取我的车辆
    private void initOkGo() {
        OkGo.<String>get(Config.s + "llc/userCar/getUserCar")
                .tag(this)
                .params("token", UserBean.getcToken(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        MyCar baseBean = gson.fromJson(response.body(), MyCar.class);
                        if (baseBean.isIsSuccess()) {
                            if (isClear){
                                mMyCars.clear();
                            }
                            mMyCars.addAll(baseBean.getData());
                            if (mMyCars.size() == 0 || mMyCars.isEmpty()) {
                                noCar.setVisibility(View.VISIBLE);
                                llCar.setVisibility(View.GONE);
                                mIvRight.setVisibility(View.VISIBLE);
                            } else {
                                noCar.setVisibility(View.GONE);
                                llCar.setVisibility(View.VISIBLE);
                                mIvRight.setVisibility(View.GONE);
                            }
                            mMyCarAdapter.notifyDataSetChanged();

                        } else {
                            noCar.setVisibility(View.VISIBLE);
                            llCar.setVisibility(View.GONE);
                        }
                    }
                });
    }

    private void initOkGoCheckCar(int id, final int pos) {
        OkGo.<String>get(Config.s + "llc/userCar/setDefaultCar")
                .tag(this)
                .params("id", id)
                .params("token", UserBean.getcToken(mContext))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        BaseBean baseBBean = gson.fromJson(response.body(), BaseBean.class);
                        if (baseBBean.isIsSuccess()) {
                            for (int i = 0; i < mMyCars.size(); i++) {
                                if (pos == i) {
                                    mMyCars.get(i).setDefaultCar(1);
                                } else {
                                    mMyCars.get(i).setDefaultCar(0);
                                }
                            }
                            mMyCarAdapter.notifyDataSetChanged();
                        } else {
                            RxToast.success("修改失败");
                        }
                    }
                });

    }

    @OnClick(R.id.tv_bangdingCar)
    public void onViewClicked() {
        if (mMyCars.size() < 3) {
            RxSPTool.putString(mContext, "SelectAddressTitle", "京A");
            Intent intent = new Intent(mContext, BindCarActivity.class);
            startActivity(intent);
        } else {
            RxToast.warning("最多可添加三辆");
        }
    }
}
